/**
 * 
 */
package net.betou.trade.main.dao.impl;

import java.util.List;

import net.betou.common.hibernate3.Finder;
import net.betou.common.hibernate3.HibernateBaseDao;
import net.betou.common.page.Pagination;
import net.betou.trade.entity.main.Role;
import net.betou.trade.main.dao.RoleDao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

/**
 * @author 叶装装
 *
 */
@Repository
public class RoleDaoImpl  extends HibernateBaseDao<Role, Integer> implements RoleDao{
	
	@Override
	protected Class< Role > getEntityClass( ) {
		return Role.class;
	}
	
	public Role findById( Integer id ){
		return get( id );
	}

	public Role deleteById( Integer id ) {
		
		Role bean = get( id );
		if( bean != null ){
			getSession( ).delete( bean );
		}
		
		return bean;
	}

	public Role findByAgentOrAdminRole( Boolean manager,  Integer id ){
		String hql = "from Role bean where bean.manager=? and bean.id=?";
		return ( Role ) findUnique( hql, manager, id );
	}
	
	public Integer findMaxLevel( Integer selfId ){
		String hql = "select max(level) from Role bean where bean.admin.id=:id";
		Query query = getSession( ).createQuery( hql );
		query.setParameter( "id", selfId );
		Object obj = query.iterate( ).next( ); 
		
		if( obj == null ){
			return 0;
		}
		
		return ( ( Number ) query.iterate( ).next( ) ).intValue( );
	}
	
	public Role findByLevel( Integer level ){
		String hql = "from Role bean where bean.level =:leve and bean.manager=:manager";
		
		Query query = getSession( ).createQuery( hql );
		query.setParameter( "leve", level );
		query.setParameter( "manager", false );
		
		return ( Role ) query.uniqueResult( );
		
	}
	
	public Role findByIdentity( String identity ){
		String hql = "from Role bean where bean.identity =?";
		return ( Role )findUniqueCacheable( hql, identity );
	}
	
	public Role findMyData( Integer selfId, Integer id ){
		String hql = "from Role bean where bean.admin.id =:adminId and bean.id=:id";
		
		Query query = getSession( ).createQuery( hql );
		query.setParameter( "adminId", selfId );
		query.setParameter( "id", id );
		
		return ( Role ) query.uniqueResult( );
	}
	
	public Role findMyDefaultRole( Integer selfId, String identity ){
		String hql = "from Role bean where bean.admin.id=? and bean.identity=?";
		return ( Role )findUnique( hql, selfId, identity );
	}
	
	@SuppressWarnings( "unchecked" )
	public List< Role > getRoleList( Boolean allowRead, Boolean isManager, Integer level, String searchProperty, String searchValue, String orderProperty, String orderDirection ){
		
		Finder f = Finder.create( "from Role bean where bean.manager=:manager " );
		f.setParam( "manager", isManager );
		
		if( level != null ){
			f.append( " and bean.level >=:level " );
			f.setParam( "level", level );
		}
		
		if( allowRead != null ){
			f.append( " and bean.allowRead=:allowRead" );
			f.setParam( "allowRead", allowRead );
		}
		
		createSearch( f, searchProperty, searchValue );
		createOrder( f, orderProperty, orderDirection, "id" );
		
		return find( f, true );
	}
	
	
	@SuppressWarnings( "unchecked" )
	public List< Role > getAgentRoleList( Integer level, Integer selfId ){
		Finder f = Finder.create( "from Role bean where bean.admin.id=:id " );
		f.setParam( "id", selfId );
		
		if( level != null ){
			f.append( " and bean.level >=:level " );
			f.setParam( "level", level );
		}
		
		
		return find( f, true );
	}

	public Role save( Role bean ) {
		getSession( ).save( bean );
		
		return bean;
	}
	
	public Pagination getAgentPage( Integer level, Integer selfId,  String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageSize, int pageNo ){
		Finder f = Finder.create( "from Role bean where bean.manager=:manager " );
		f.setParam( "manager", false );
		
		if( selfId != null ){
			f.append( " and bean.admin.id=:selfId" );
			f.setParam( "selfId", selfId );
		}
		
		if( level != null ){
			f.append( " and bean.level >=:level " );
			f.setParam( "level", level );
		}
		
		
		createSearch( f, searchProperty, searchValue );
		createOrder( f, orderProperty, orderDirection, "id" );
		
		
		return this.find( f, pageNo, pageSize );
		
	}
	
	@SuppressWarnings( "unchecked" )
	public List< Role > getAgentRoleListByIdentity( String identity ){
		Finder f = Finder.create( "from Role bean where bean.identity=:identity " );
		f.setParam( "identity", identity );
		return find( f, true );
	}


}
